package com.dingpeikeji.quickdeer.modules.site.web;

import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.dingpeikeji.quickdeer.core.modules.site.entity.TSitedtl;
import com.dingpeikeji.quickdeer.core.modules.site.service.TSitedtlService;
import com.dingpeikeji.quickdeer.modules.utils.ValidateTool;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;

@Controller
@RequestMapping("/f/info/site")
public class FSiteController {
	@Autowired
	private TSitedtlService sitedtlService;
	
	@RequestMapping("siteJson")
	@ResponseBody
	public List<String> siteJson(String spottypes) {
		//['北京市#001#1|beijing|bj','上海市#002#0|shanghai|sh'];
		TSitedtl query = new TSitedtl();
		if(!ValidateTool.isEmptyOrNull(spottypes)) {
			query.setSpottypes(spottypes.split(","));
		}
		List<TSitedtl> siteList = sitedtlService.findList(query);
		List<String> data = Lists.newArrayList();
		// 对每个数据集进行格式化
		if(siteList!=null && !siteList.isEmpty()) {
			for(TSitedtl site : siteList) {
				String[] querycode = site.getQuerycode().split(" ");
				String firstC = "";
				for(String c:querycode) {
					firstC += c.substring(0, 1);
				}
				String item = site.getSitename()+
						"#"+site.getSiteid()+
						"#"+site.getIshot()+
						"|"+site.getQuerycode().replace(" ", "")+
						"|"+firstC;						
				data.add(item);
			}
		}
		
		return data;
	}
	@RequestMapping("getSiteId")
	public String getSiteId(String city) {
		System.out.println(city);
		return null;
	}
	@RequestMapping("siteAndCityJson")
	@ResponseBody
	public List<String> siteAndCityJson(String spottypes, String city) {
		
		TSitedtl query = new TSitedtl();
		if(!ValidateTool.isEmptyOrNull(spottypes)) {
			query.setSpottypes(spottypes.split(","));
		}
		query.setSiteid(city);
		List<TSitedtl> siteList = sitedtlService.findList(query);
		List<String> data = Lists.newArrayList();
		// 对每个数据集进行格式化
		if(siteList!=null && !siteList.isEmpty()) {
			for(TSitedtl site : siteList) {
				String[] querycode = site.getQuerycode().split(" ");
				String firstC = "";
				for(String c:querycode) {
					firstC += c.substring(0, 1);
				}
				String item = site.getSitename()+
						"#"+site.getSiteid()+
						"#"+site.getIshot()+
						"|"+site.getQuerycode().replace(" ", "")+
						"|"+firstC;						
				data.add(item);
			}
		}
		
		return data;
	}
}
